home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / faq / wdj0597.zip / TECHTIPS.ZIP / MASHLAN.ZIP / BT.C next >
C/C++ Source or Header  |  1997-01-31  |  1KB  |  61 lines

  1. // listing 1 bt.c
  2. // note - make sure the multi-threaded library option
  3. // is on in your compiler's IDE settings
  4.  
  5. #include <windows.h>
  6. #include <process.h>
  7. #include <stdio.h>
  8.  
  9. HANDLE beginthread_handle(
  10. #ifdef __BORLANDC__
  11.    void _USERENTRY (*start_address)(void *),
  12. #else
  13.    unsigned (__stdcall *start_address)(void *),
  14. #endif
  15.    unsigned stack_size,
  16.    void *arglist )
  17. {
  18.    DWORD id;
  19.    HANDLE h1 = (HANDLE)
  20. #ifdef __BORLANDC__
  21.         _beginthreadNT(start_address,stack_size,
  22.                 arglist,NULL,CREATE_SUSPENDED,&id);
  23. #else
  24.         // for Visual C++ and compatibles
  25.         _beginthreadex(NULL,stack_size,start_address,
  26.                arglist,CREATE_SUSPENDED,&id);
  27. #endif
  28.    HANDLE h2 = INVALID_HANDLE_VALUE;
  29.    if( h1 != INVALID_HANDLE_VALUE ) {
  30.       DuplicateHandle(GetCurrentProcess(),h1,
  31.           GetCurrentProcess(),&h2,0,FALSE,
  32.           DUPLICATE_SAME_ACCESS);
  33.       ResumeThread(h1);
  34.    }
  35.    return h2;
  36. }
  37.  
  38. #ifdef __BORLANDC__
  39. void thread( void *arg )
  40. #else
  41. unsigned __stdcall thread( void * arg )
  42. #endif
  43. {
  44.    printf("new thread called\n");
  45. #ifndef __BORLANDC__
  46.    return 0;
  47. #endif
  48. }
  49.  
  50. int main(void)
  51. {
  52.    HANDLE h = beginthread_handle(thread,4096,NULL);
  53.    if(h!=INVALID_HANDLE_VALUE) {
  54.        printf("waiting on new thread\n");
  55.        WaitForSingleObject(h,INFINITE);
  56.        printf("new thread is signaled\n");
  57.        CloseHandle(h);
  58.    }
  59.    return 0;
  60. }
  61.